<?php

namespace App\Jobs\Posts;

use App\Models\WxAttachment;
use App\Wen\Utils\ImageUtils;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class UpdatePosterGlassJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $post_id = 0;
    public $tries = 1;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($post_id)
    {
        //
        $this->post_id = $post_id;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //
        if(!_empty_($this->post_id)){
            $poster_glass = get_post_meta($this->post_id, 'post_poster_glass_obj', 'j');
            if($poster_glass){
                $attach_id = $poster_glass['attach_id'];
                if($attach_id > 0){
                    $attachment = WxAttachment::find($attach_id);
                    if($attachment){
                        $url = $attachment->domain . '/' . $attachment->path;
                        if($url){
                            $poster_glass['theme'] = ImageUtils::is_mostly_white($url) ? 'white' : 'black';
                            update_post_meta($this->post_id, 'post_poster_glass_obj', $poster_glass, 'j');
                        }
                    }
                }
            }
        }
    }
}
